import { createApp } from 'vue';
import App from './App.vue';
import { App as MicroApp } from './app/app';
import { setupStore } from '@/store';
import { router, setupRouter } from '@/router';
import { setupRouterGuard } from '@/router/guard';
import { Interceptors } from '@/http/interceptor';
import { useInstallApp } from '@/hooks/use-app';
import {
	registerCommonComp,
	registerPageComp,
	registerDirective,
	registerUserComp,
} from '@/register';
import { setupI18N } from '@/locales';
if (import.meta.env.DEV) {
	import('ant-design-vue/dist/antd.less');
}
import 'font-awesome/css/font-awesome.css';
import '@/styles/index.less';
import less from 'less'; //根据配置文件更改颜色变量需要引用less，不然window.less为undefined;
import VueAMap from 'vue-amap';
// import AMapLoader from '@amap/amap-jsapi-loader';
import renderWithQiankun, { qiankunWindow } from 'vite-plugin-qiankun/dist/helper';
import { GlobalState } from './micro';
import { AppConfig } from './config';
// AMapLoader.load({
// 	key: '6ab2751103aea67e817c90a5528181b5',
// 	version: '2.0',
// 	plugins: ['AMap.Geolocation', 'AMap.PlaceSearch', 'AMap.Geocoder'],
// })
let app: any = null;
async function bootstrap(props?: any) {
	app = createApp(App);
	if (import.meta.env.DEV) {
		const Antd = await import('ant-design-vue');
		app.use(Antd);
	}
	//挂载地图
	app.use(VueAMap);
	//挂载less
	app.use(less);
	//  安装应用
	useInstallApp();
	//  设置国际化
	await setupI18N(app);
	//  配置store
	setupStore(app);
	//  设置路由
	setupRouter(app);
	//  设置路由守卫
	setupRouterGuard(router);
	//  设置拦截器
	Interceptors.getInstance(router);
	// 组件注册
	registerCommonComp(app);
	registerUserComp(app);
	registerPageComp(app);
	registerDirective(app);
	app.mount(props?.container ? props.container : '#app');
	return app;
}

if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
	bootstrap();
} else {
	const initGlobalState = MicroApp.getInstance().startMicroApp();
	renderWithQiankun({
		mount(props:any) {
			bootstrap(props);
			initGlobalState(new GlobalState(props));
		},
		bootstrap() {
			console.log(
				`%c子应用${AppConfig.sysName}_${AppConfig.appName}执行bootstrap操作`,
				'color:green'
			);
		},
		update() {
			console.log(
				`%c子应用${AppConfig.sysName}_${AppConfig.appName}执行update操作`,
				'color:green'
			);
		},
		unmount() {
			console.log(
				`%c子应用${AppConfig.sysName}_${AppConfig.appName}执行unmount操作`,
				'color:green'
			);
			app.unmount();
		},
	});
}
